package com.yisu.expressway.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.yisu.expressway.R;
import com.yisu.expressway.service_district.ShopDetailActivity;
import com.yisu.expressway.model.ServiceDistrictObj;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/10/5 0005.
 */

public class ShopListAdapter extends  RecyclerView.Adapter<ShopListAdapter.ShopViewHolder> {


    ArrayList<ServiceDistrictObj.ShopItemObj> mData = new ArrayList<>();
    private Context mContext;
    private int mServiceAreaId;

    public ShopListAdapter (Context context ,int serviceAreaId){
        this.mContext = context;
        mServiceAreaId = serviceAreaId;
    }

    public void setData(ArrayList<ServiceDistrictObj.ShopItemObj> list){
        this.mData = list;
        notifyDataSetChanged();
    }

    @Override
    public ShopViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View mView = LayoutInflater.from(mContext).inflate(R.layout.shop_item, parent, false);
        return new ShopViewHolder(mView);
    }

    @Override
    public void onBindViewHolder(ShopViewHolder holder, int position) {

        if(mData.size() <= position ){
            return;
        }

        final ServiceDistrictObj.ShopItemObj item = mData.get(position);

        Glide.with(mContext)
                .load(item.logoUrl)
                .placeholder(R.drawable.blank)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .centerCrop()
                .into(holder.mIv_logo);

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ShopDetailActivity.launch(mContext,mServiceAreaId,item.id,item.shopName);
            }
        });
    }


    @Override
    public int getItemCount() {
        return mData.size();
    }

    class ShopViewHolder extends RecyclerView.ViewHolder {

        @Bind(R.id.iv_logo) ImageView mIv_logo;


        public ShopViewHolder(View itemView){
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }
}
